<?php
namespace Lib\Cate;
class Level{
	/**
	 * 无限级分类
	 * @param  [type]  $cate  [description]
	 * @param  string  $html  [description]
	 * @param  integer $pid   [description]
	 * @param  integer $level [description]
	 * @return [type]         [description]
	 */
	static public function unlimitedForLevel($cate,$html = '--',$pid = 0,$level = 0){
		$arr = array();
		foreach ($cate as $v) {
			if($v['pid'] == $pid){
				$v['level'] = $level + 1;
				$v['html'] = str_repeat($html, $level);
				$arr[] = $v;
				$arr = array_merge($arr, self::unlimitedForLevel($cate,$html,$v['id'],$level+1));
			}
		}
		return $arr;
	}
	/**
	 * 传递父级分类id返回所有子分类id
	 * @param  [type] $cate [description]
	 * @param  [type] $pid  [description]
	 * @return [type]       [description]
	 */
	static public function getChildsId($cate,$pid){
		$arr = array();
		foreach($cate as $v){
			if($v['pid'] == $pid){
				$arr[] = $v['id'];
				$arr = array_merge($arr,self::getChildsId($cate,$v['id']));
			}
		}
		return $arr;
	}
	/**
	 * 传递父级分类id返回所有子分类
	 * @param  [type] $cate [description]
	 * @param  [type] $pid  [description]
	 * @return [type]       [description]
	 */
	static public function getChilds($cate,$pid){
		$arr = array();
		foreach($cate as $v){
			if($v['pid'] == $pid){
				$arr[] = $v;
				$arr = array_merge($arr,self::getChilds($cate,$v['id']));
			}
		}
		return $arr;
	}
}